using System;
using System.Text;
using System.Runtime.InteropServices;
using PlateDSP;

namespace PlateDSP
{
    public class CPlateDSP : CAPI4PlateDSPBase
    {
        public CPlateDSP()
        {
        }
        ~CPlateDSP()
        {
        }

	    public virtual void AfterDvrClosed()	{}
        public virtual void AfterRecogFinished(int PlateNum) { }
        public virtual void AfterFilterStateChanged(int Event, UInt32 Target) { }
        public virtual void AfterImageSizeChanged(UInt32 Width, UInt32 Height) { }
        public virtual void AfterMotionStateChanged(int Event, UInt32 MotionTarget) { }
        public virtual void AfterRedLampStateChanged(int Color, UInt32 LampIndex) { }
        public virtual void AfterGetJpgSourceData(IntPtr pData, UInt32 Size) { }
        public virtual void AfterCompressedJpgData(IntPtr pData, UInt32 Size) { }

        public override void Callback(PLATEDSP_MSG Msg)
	    {
		    switch( Msg.Type )
		    {
		    case DSP_RECOG_EVENT:
			    if( Msg.Event <= DSP_RECOG_HAS_RESULT )
			    {
				    AfterRecogFinished(Msg.Event);
			    }
			    else
			    {
				    AfterFilterStateChanged(Msg.Event,Msg.Target);
			    }
			    break;
		    case DSP_FILE_EVENT:
			    switch(Msg.Event)
			    {
			    case DSP_DVR_CLOSED:
    				AfterDvrClosed();
	    			break;
			    }
			    break;
		    case DSP_IMAGE_SIZE_CHANGED_EVENT:
    			AfterImageSizeChanged(Msg.Width,Msg.Height);
			    break;
		    case DSP_MOTION_EVENT:
    			AfterMotionStateChanged(Msg.Event,Msg.Target);
			    break;
		    case DSP_RED_LAMP_EVENT:
    			AfterRedLampStateChanged(Msg.LampColor,Msg.Target);
			    break;
		    case DSP_JPG_SOURCE_EVENT:
    			AfterGetJpgSourceData(Msg.pData,Msg.Size);
			    break;
		    case DSP_JPG_COMPRESSED_EVENT:
    			AfterCompressedJpgData(Msg.pData,Msg.Size);
			    break;
		    }
	    }

	    public bool Open(String pGuiName)
	    {
		    ResetStatTargetSavedFlags();
		    return OpenBase(pGuiName);
	    }
        public bool OpenEx(String pGuiName,IntPtr pObject)
        {
            ResetStatTargetSavedFlags();
            return OpenBaseEx(pGuiName,pObject);
        }
        public void Close()
        {
            CloseBase();
        }
	    public bool StatGetTargetSavedFlags(UInt32 TargetId)
	    {
    		return bSaved[TargetId];
	    }
	    public void StatSetTargetSavedFlags(UInt32 TargetId,bool bState)
	    {
    		bSaved[TargetId] = bState;
	    }
        private	void ResetStatTargetSavedFlags()
	    {
		    for(int i=0;i<DSP_MAX_TARGET_NUM; i++)
		    {
			    bSaved[i] = false;
            }
		}
        private bool[] bSaved = new bool[DSP_MAX_TARGET_NUM];
    }
}
